package com.globallogic.university.documents;

import java.util.ArrayList;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.globallogic.university.events.Event;
import com.globallogic.university.events.Lecture;
import com.globallogic.university.events.Seminar;
import com.globallogic.university.events.Event.DaysOfWeek;
import com.globallogic.university.people.StudentGroup;
import com.globallogic.university.people.Teacher;
import com.globallogic.university.subject.Subject;
import com.globallogic.university.venues.Auditorium;

public class ScheduleTest {
	
	@Test
	public void shouldAddLectureToSchedule() {
		// given
		Schedule schedule = new Schedule();
		Auditorium auditorium = new Auditorium("333");
		Subject subject = new Subject("Physics");
		Teacher teacher = new Teacher("Albert", "Einstein");
		StudentGroup group = new StudentGroup("Group1");
		schedule.addGroupToSchedule(group);
		
		Lecture lecture = new Lecture(group, DaysOfWeek.THURSDAY, 3, auditorium, subject, teacher);
				
		// when
		schedule.addEvent(lecture);
		
		// then
		Assert.assertTrue(schedule.getEventsList().contains(lecture));
	}
	
	@Test
	public void shouldAddSeminarToSchedule() {
		// given	
		Schedule schedule = new Schedule();
		Auditorium auditorium = new Auditorium("333");
		Subject subject = new Subject("Algebra");
		Teacher teacher = new Teacher("Albert", "Einstein");
		StudentGroup group = new StudentGroup("Group1");
		schedule.addGroupToSchedule(group);
		
		Seminar seminar = new Seminar(group, DaysOfWeek.TUESDAY, 5, auditorium, subject, teacher);
		
		// when
		schedule.addEvent(seminar);
		
		// then
		Assert.assertTrue(schedule.getEventsList().contains(seminar));
	}
	
	@Test
	public void shouldShowWeekSchedule() {
		// given
		String auditoriumID1 = "333";
		String auditoriumID2 = "433";
		String subjectName = "Physics";
		String firstName = "Albert";
		String lastName = "Einstein";
		String groupId1 = "Group1";
		String groupId2 = "Group2";
		
		Schedule schedule = new Schedule();
		Auditorium auditorium1 = new Auditorium(auditoriumID1);
		Auditorium auditorium2 = new Auditorium(auditoriumID2);
		Subject subject = new Subject(subjectName);
		Teacher lecturer = new Teacher(firstName, lastName);
		StudentGroup group1 = new StudentGroup(groupId1);
		schedule.addGroupToSchedule(group1);
		StudentGroup group2 = new StudentGroup(groupId2);
		schedule.addGroupToSchedule(group2);
		
		Lecture lecture1 = new Lecture(group1, DaysOfWeek.MONDAY, 1, auditorium1, subject, lecturer);
		schedule.addEvent(lecture1);
		
		Seminar seminar1 = new Seminar(group1, DaysOfWeek.TUESDAY, 2, auditorium2, subject, lecturer); 
		schedule.addEvent(seminar1);
		
		Lecture lecture2 = new Lecture(group2, DaysOfWeek.THURSDAY, 3, auditorium1, subject, lecturer);
		schedule.addEvent(lecture2);
		
		Seminar seminar2 = new Seminar(group2, DaysOfWeek.FRIDAY, 5, auditorium2, subject, lecturer);
		schedule.addEvent(seminar2);
			
		// when
		String actual = schedule.toString();
		System.out.println(actual);
		
		// then
		String expected = String.format("SCHEDULE FOR GROUP %s:\n" + 
				"MONDAY:\n" +
				"Lecture: Slot [1] Auditorium [%s] Subject [%s] Teacher [%s %s]\n" +
				"TUESDAY:\n" +
				"Seminar: Slot [2] Auditorium [%s] Subject [%s] Teacher [%s %s]\n" +
				"WEDNESDAY:\n" +
				"THURSDAY:\n" +
				"FRIDAY:\n",
				groupId1,
				auditoriumID1, subjectName, firstName, lastName,
				auditoriumID2, subjectName, firstName, lastName);
		expected += String.format("SCHEDULE FOR GROUP %s:\n" + 
				"MONDAY:\n" +
				"TUESDAY:\n" +
				"WEDNESDAY:\n" +
				"THURSDAY:\n" +
				"Lecture: Slot [3] Auditorium [%s] Subject [%s] Teacher [%s %s]\n" +
				"FRIDAY:\n" +
				"Seminar: Slot [5] Auditorium [%s] Subject [%s] Teacher [%s %s]\n",
				groupId2,
				auditoriumID1, subjectName, firstName, lastName,
				auditoriumID2, subjectName, firstName, lastName);
		Assert.assertEquals(actual, expected);	
	}
	
	@Test
	public void shouldIndicateMissingPeerEvent() {
		// given
		String auditoriumID1 = "333";
		String subjectName = "Physics";
		String firstName = "Albert";
		String lastName = "Einstein";
		String groupId1 = "Group1";
		String groupId2 = "Group2";
		
		Schedule schedule = new Schedule();
		Auditorium auditorium1 = new Auditorium(auditoriumID1);
		Subject subject = new Subject(subjectName);
		Teacher lecturer = new Teacher(firstName, lastName);
		StudentGroup group1 = new StudentGroup(groupId1);
		StudentGroup group2 = new StudentGroup(groupId2);
		schedule.addGroupToSchedule(group1);
		schedule.addGroupToSchedule(group2);

		Seminar seminar = new Seminar(group1, DaysOfWeek.TUESDAY, 2, auditorium1, subject, lecturer); 
		schedule.addEvent(seminar);
		
		Lecture lecture= new Lecture(group2, DaysOfWeek.THURSDAY, 3, auditorium1, subject, lecturer); 
		schedule.addEvent(lecture);
		
		// when
		String actual = schedule.toString();
		System.out.println(actual);
		
		// then
		String expected = String.format("SCHEDULE FOR GROUP %s:\n" + 
				"MONDAY:\n" +
				"TUESDAY:\n" +
				"Seminar: Slot [2] Auditorium [%s] Subject [%s] Teacher [%s %s] !!MISSING PEER EVENT!!\n" +
				"WEDNESDAY:\n" +
				"THURSDAY:\n" +
				"FRIDAY:\n",
				groupId1,
				auditoriumID1, subjectName, firstName, lastName);
		expected += String.format("SCHEDULE FOR GROUP %s:\n" + 
				"MONDAY:\n" +
				"TUESDAY:\n" +
				"WEDNESDAY:\n" +
				"THURSDAY:\n" +
				"Lecture: Slot [3] Auditorium [%s] Subject [%s] Teacher [%s %s] !!MISSING PEER EVENT!!\n" +
				"FRIDAY:\n",
				groupId2,
				auditoriumID1, subjectName, firstName, lastName);
		Assert.assertEquals(actual, expected);		
	}
	
	@Test
	public void shouldNotHaveMultipleEventsInTimeSlot() {
		// given
		DaysOfWeek dayOfWeek = DaysOfWeek.THURSDAY;
		int timeSlotNumber = 5;
		
		Schedule schedule = new Schedule();

		Auditorium auditorium = new Auditorium("333");
		Teacher teacher = new Teacher("Albert", "Einstein");
		
		Subject subject1 = new Subject("Law");
		StudentGroup group1 = new StudentGroup("Group1");
		schedule.addGroupToSchedule(group1);
		
		Subject subject2 = new Subject("History");
		StudentGroup group2 = new StudentGroup("Group1");
		schedule.addGroupToSchedule(group2);
		
		Lecture event1 = new Lecture(group1, dayOfWeek, timeSlotNumber, auditorium, subject1, teacher);
		schedule.addEvent(event1);

		// when
		Seminar event2 = new Seminar(group2, dayOfWeek, timeSlotNumber, auditorium, subject2, teacher);
		schedule.addEvent(event2);

		// then
		ArrayList<Event> expectedEvents = schedule.getEventByTimeSlot(dayOfWeek, timeSlotNumber);
		Assert.assertEquals(expectedEvents.size(), 1); // only one event for the time slot is returned
		Assert.assertEquals(expectedEvents.get(0), event1); // the event that was added to the time slot first is in the schedule
	}	
}
